package shg.book.fitment
{
	import flash.display.BitmapData;
	import flash.display.CapsStyle;
	import flash.display.GradientType;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.HTML;
	import mx.controls.Image;
	import mx.core.BitmapAsset;
	import mx.core.DragSource;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import shg.book.database.ConnectionDatabase;
	import shg.book.read.ReadTextBookAction;
	import shg.book.size.BookData;
	import shg.book.util.ImageLoader;
	
	import spark.components.BorderContainer;
	import spark.filters.DropShadowFilter;
	
	/**
	 * 这是打开书皇馆的显示样式设计！
	 * 用户也可以打开程序后在装修房间！
	 * */
	public class FitmentSHG
	{
		static public var flexBook:shuhg;
		private var imageLoadercenter:ImageLoader;
		private var imageLoaderUp:ImageLoader;
		private var imageLoaderDown:ImageLoader;
		private var imageLoaderLeft:ImageLoader;
		private var imageLoaderRight:ImageLoader;
		private var imageLoaderHead:ImageLoader;
		private var imageLoaderName:ImageLoader;
		private var imageLoaderHuaHua:ImageLoader;
		private var imageLoaderHuaHuaV:ImageLoader;
		private var imageLoaderDrawBook:ImageLoader;
		private var readTextBook:ReadTextBookAction;
		//投影
		private var filterOne:ImageLoader;
		private var filterTwo:ImageLoader;
		private var filterThree:ImageLoader;
		private var filterFour:ImageLoader;
		//数据2边
		private var bothSidesLeft:ImageLoader;
		private var bothSidesRight:ImageLoader;
		//图片的ID标识符
		private var imageID:String;
		//本地数据库操作类
		private var connectionDatabase:ConnectionDatabase;
		/**
		 * 这是当用户刚刚启动阅读器的是去读取文件
		 * 里面的初始化数据信息，初始化信息有两种方式读取。一种是读取本地文件了里面的数据对阅读器进行初始化
		 * 一种是从远程服务器读取文件进行初始化，（如果本地没有才从服务器读取）
		 * */
		[Bindable]
		private var bitmapData:BitmapData;
		private var newImageX:Number=0;
		private var newImageY:Number=0;
		//当点击图片的时候鼠标所点击的位置在当前点击这张图片的X轴和Y轴
		private var clickX:Number=0;
		private var clickY:Number=0;
		//投影
		private var myDropShadowFilter:spark.filters.DropShadowFilter;  
		private var color:Number = 0x000000;
		
		private var image:Image;
		//移动之前图片所做的X和Y轴
		private var _oldX:Number=0;
		private var _oldY:Number=0;
		public function FitmentSHG()
		{
		}
		public function initApp():void{
			//投影
			myDropShadowFilter = new DropShadowFilter(15, 180, 
				color, 0.9, 40, 8, 0.65, BitmapFilterQuality.HIGH, false, false,false);
			
			imageLoadercenter=new ImageLoader("assets/main/book.gif");
			imageLoadercenter.addEventListener(Event.COMPLETE, fitmentCenter);
			imageLoaderUp=new ImageLoader("assets/main/ReadBorderUp.gif");
			imageLoaderUp.addEventListener(Event.COMPLETE, fitmentUp);
			imageLoaderDown=new ImageLoader("assets/main/ReadBorderDown.gif");
			imageLoaderDown.addEventListener(Event.COMPLETE, fitmentDown);
			imageLoaderLeft=new ImageLoader("assets/main/ReadBorderLeft.gif");
			imageLoaderLeft.addEventListener(Event.COMPLETE, fitmentLeft);
			imageLoaderRight=new ImageLoader("assets/main/ReadBorderRight.gif");
			imageLoaderRight.addEventListener(Event.COMPLETE, fitmentRight);
			imageLoaderHead=new ImageLoader("assets/main/head.gif");
			imageLoaderHead.addEventListener(Event.COMPLETE, fitmentHead);
			imageLoaderName=new ImageLoader("assets/main/SHG.png");
			imageLoaderName.addEventListener(Event.COMPLETE, fitmentName);
			//投影
			filterOne=new ImageLoader("assets/main/Border.gif");
			filterOne.addEventListener(Event.COMPLETE, filter_One);
			filterTwo=new ImageLoader("assets/main/Border.gif");
			filterTwo.addEventListener(Event.COMPLETE, filter_Two);
			filterThree=new ImageLoader("assets/main/Border.gif");
			filterThree.addEventListener(Event.COMPLETE, filter_Three);
			filterFour=new ImageLoader("assets/main/Border.gif");
			filterFour.addEventListener(Event.COMPLETE, filter_Four);
			//书架两边
			bothSidesLeft=new ImageLoader("assets/main/BothSides.gif");
			bothSidesLeft.addEventListener(Event.COMPLETE, bothSides_Left);
			bothSidesRight=new ImageLoader("assets/main/BothSides.gif");
			bothSidesRight.addEventListener(Event.COMPLETE, bothSides_Right);
		}
		
		/**
		 * 装修书皇馆中心书架
		 * 340：指的是加宽书架，这个数据越大书架就会越宽
		 * 260：指的是加高书架，这个数据越大书架就会越高
		 * 350：指定书架的左上角的X坐标位置，数据阅读书架就越靠右
		 * 120：指的是书架的左上角的Y轴坐标，数据越大书架就越下移
		 * */
		private function fitmentCenter(event:Event):void{
			//var img:Image=makeBorder(340,340,340,340,255,255,255,255,340,230,imageLoadercenter);
			//flexBook.centerShetf.addChild(img);
		}
		private function fitmentUp(event:Event):void{
			var img:Image=makeBorder(450,450,430,430,10,10,10,10,440,10,imageLoaderUp);
			flexBook.mainUp.addChild(img);
		}
		private function fitmentDown(event:Event):void{
			var img:Image=makeBorder(430,430,450,450,10,10,10,10,450,30,imageLoaderDown);//makeImg(900/2,10,imageLoaderDown);
			flexBook.mainDown.addChild(img);
		}
		private function fitmentLeft(event:Event):void{
			var img:Image=makeImg(10,flexBook.height/2,imageLoaderLeft);
			flexBook.mainLeft.addChild(img);
		}
		private function fitmentRight(event:Event):void{
			var img:Image=makeImg(10,flexBook.height/2,imageLoaderRight);
			flexBook.mainRight.addChild(img);
		}
		private function fitmentHead(event:Event):void{
			//var img:Image=makeImg(flexBook.headBg.width/2,flexBook.headBg.height/2,imageLoaderHead);
			//flexBook.headBg.addChild(img);
		}
		private function fitmentName(event:Event):void{
			//var img:Image=makeImg(flexBook.mainToolBar.shgName.width/2,flexBook.mainToolBar.shgName.height/2,imageLoaderName);
			//flexBook.mainToolBar.shgName.addChild(img);
		}
		private function filter_One(event:Event):void{
			var img:Image=makeImg(flexBook.filterOne.width/2,flexBook.filterOne.height/2,filterOne);
			flexBook.filterOne.addElement(img);
		}
		private function filter_Two(event:Event):void{
			var img:Image=makeImg(flexBook.filterTwo.width/2,flexBook.filterTwo.height/2,filterTwo);
			flexBook.filterTwo.addElement(img);
		}
		private function filter_Three(event:Event):void{
			var img:Image=makeImg(flexBook.filterThree.width/2,flexBook.filterThree.height/2,filterThree);
			flexBook.filterThree.addElement(img);
		}
		private function filter_Four(event:Event):void{
			var img:Image=makeImg(flexBook.filterFour.width/2,flexBook.filterFour.height/2,filterFour);
			flexBook.filterFour.addElement(img);
		}
		private function bothSides_Left(event:Event):void{
			var img:Image=makeImg(flexBook.bothSidesLeft.width/2,flexBook.bothSidesLeft.height/2,bothSidesLeft);
			flexBook.bothSidesLeft.addElement(img);
		}
		private function bothSides_Right(event:Event):void{
			var img:Image=makeImg(flexBook.bothSidesRight.width/2,flexBook.bothSidesRight.height/2,bothSidesRight);
			flexBook.bothSidesRight.addElement(img);
		}
		/**
		 * image的name属性将会组成一个
		 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&&C://book//来生缘.shg
		 * 这样的字符串
		 * */
		public function fitmentBook(arrayCover:ArrayCollection):void{
			for each(var item in arrayCover){
				var _PX=item._PX;
				var _PY=item._PY;
				var img:Image=new Image();
				img.name=item.id;
				img.name+="&&"+item.filePath;
				img.source=item.imageByte;
				img.x=_PX;
				img.y=_PY;
				img.width=75;
				img.height=100;
				updateBookSize(_PX,_PY,"add");
				//设置手型
				img.buttonMode=true;
				img.mouseChildren=false;
				img.useHandCursor=true;
				//使双击生效
				img.doubleClickEnabled=true;
				img.addEventListener(MouseEvent.DOUBLE_CLICK,openBook);
				
				img.addEventListener(MouseEvent.MOUSE_MOVE,mouseOverHandler);
				flexBook.newImagePosition.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
				flexBook.newImagePosition.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
				flexBook.newImagePosition.addEventListener(DragEvent.DRAG_EXIT,dragExitHandler);
				flexBook.newImagePosition.addElement(img);
			}
		}
		private function hideCover(event:MouseEvent):void{
			event.currentTarget.visible=false;
		}
		private function showCover(event:MouseEvent):void{
			event.currentTarget.visible=true;
		}
		/**
		 * 因为书架的每层的宽度为650，而每本书的宽度为90，所以
		 * 每层书架只能显示6本书，每本书所需的空间为约等于108
		 * 
		 * */
		private function newPosition(event:MouseEvent):void{
			newImageX=event.localX;
			newImageY=event.localY;
			var _bookNumber:String="";
			var _X:int=0;
			var _Y:int=0;
			if(newImageY-clickY<115){
				_Y=0;
			}else if(newImageY-clickY<230){
				_Y=120;
			}else if(newImageY-clickY<345){
				_Y=240;
			}else{
				_Y=360;
			}
			if(newImageX-clickX<81){
				_X=0;
			}else if(newImageX-clickX<162){
				_X=81;
			}else if(newImageX-clickX<243){
				_X=162;
			}else if(newImageX-clickX<324){
				_X=243;
			}else if(newImageX-clickX<405){
				_X=324;
			}else if(newImageX-clickX<486) {
				_X=405;
			}else if(newImageX-clickX<567){
				_X=486;
			}else {
				_X=567;
			}
			
			if(newImageX-clickX >0){
				image.x= _X;
				image.y=_Y;
			}else{
				image.x= _X;
				image.y=0;
			}
			//对书架的册数进行调整,加
			updateBookSize(_X,_Y,"add");
			//对书架的册数进行调整,减
			updateBookSize(_oldX,_oldY,"subtract");
			//删除这个监听事件以免对后面的操作产生影响
			event.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,newPosition);
			//当用户拖动某本书籍的时候就需要到数据库更新它的X和Y轴
			if(connectionDatabase==null)
				connectionDatabase=new ConnectionDatabase();
			//connectionDatabase.update(imageID,_X,_Y);
		}
		
		
		private function mouseOverHandler(event:MouseEvent):void 
		{  
			_oldX=event.currentTarget.x;
			_oldY=event.currentTarget.y;
			var dragInitiator:Image=Image(event.currentTarget);
			var ds:DragSource = new DragSource();
			ds.addData(dragInitiator, "img");               
			var proxyImage:BitmapAsset= new BitmapAsset();
			proxyImage.bitmapData=new BitmapData(dragInitiator.width,dragInitiator.height);
			proxyImage.bitmapData.draw(dragInitiator);
			DragManager.doDrag(dragInitiator, ds, event,proxyImage,0,0,1);
		}
		/**
		 * 当点击某张书籍图片的时候
		 * */
		private function openBook(event:MouseEvent):void{
			BookData.isInsert=true;
			readTextBook=new ReadTextBookAction();
			readTextBook.name=event.currentTarget.name.toString().split("&&")[0];
			BookData.filePath=event.currentTarget.name.toString().split("&&")[1];
			readTextBook.selectFile();
		}
		private function dragDropHandler(event:DragEvent):void{
			event.dragInitiator.alpha=1;
			clickX=event.dragInitiator.mouseX;
			clickY=event.dragInitiator.mouseY;
			image=Image(event.dragInitiator);
			imageID=event.dragInitiator.name.split("&&")[0];
			flexBook.newImagePosition.addEventListener(MouseEvent.MOUSE_UP,newPosition);
		}
		private function dragEnterHandler(event:DragEvent):void{
			event.dragInitiator.alpha=0;
			if (event.dragSource.hasFormat("img"))
			{
				DragManager.acceptDragDrop(BorderContainer(event.currentTarget));
			}
		}
		private function dragExitHandler(event:DragEvent):void{
			event.dragInitiator.alpha=1;
		}
		/**
		 * 通过传经来的这四个数据可以计算出当前这种图片是从那个位置
		 * 移动到那个位置
		 * oldX原来的所做的X轴
		 * oldY原来所在的Y轴
		 * newX新位置的X轴
		 * newY新位置的Y轴
		 * */
		private function updateBookSize(_XX:Number,_YY:Number,_str:String):void{
			/**var _text:String="";
			if(_YY<119){
				_text="one";
			}else if(_YY<239){
				_text="two";
			}else if(_YY<349){
				_text="three";
			}else{
				_text="four";
			}
			
			if(_XX<81){
				_text+="_one_text";
				if(_text=="one_one_text"){
					if(_str=="add"){
						flexBook.one_one_text+=1;
					}else{
						flexBook.one_one_text-=1;
					}
				}else if(_text=="two_one_text"){
					if(_str=="add"){
						flexBook.two_one_text+=1;
					}else{
						flexBook.two_one_text-=1;
					}
				}else if(_text=="three_one_text"){
					if(_str=="add"){
						flexBook.three_one_text+=1;
					}else{
						flexBook.three_one_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_one_text+=1;
					}else{
						flexBook.four_one_text-=1;
					}
				}
			}else if(_XX<162){
				_text+="_two_text";
				if(_text=="one_two_text"){
					if(_str=="add"){
						flexBook.one_two_text+=1;
					}else{
						flexBook.one_two_text-=1;
					}
				}else if(_text=="two_two_text"){
					if(_str=="add"){
						flexBook.two_two_text+=1;
					}else{
						flexBook.two_two_text-=1;
					}
				}else if(_text=="three_two_text"){
					if(_str=="add"){
						flexBook.three_two_text+=1;
					}else{
						flexBook.three_two_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_two_text+=1;
					}else{
						flexBook.four_two_text-=1;
					}
				}
			}else if(_XX<243){
				_text+="_three_text";
				if(_text=="one_three_text"){
					if(_str=="add"){
						flexBook.one_three_text+=1;
					}else{
						flexBook.one_three_text-=1;
					}
				}else if(_text=="two_three_text"){
					if(_str=="add"){
						flexBook.two_three_text+=1;
					}else{
						flexBook.two_three_text-=1;
					}
				}else if(_text=="three_three_text"){
					if(_str=="add"){
						flexBook.three_three_text+=1;
					}else{
						flexBook.three_three_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_three_text+=1;
					}else{
						flexBook.four_three_text-=1;
					}
				}
			}else if(_XX<324){
				_text+="_four_text";
				if(_text=="one_four_text"){
					if(_str=="add"){
						flexBook.one_four_text+=1;
					}else{
						flexBook.one_four_text-=1;
					}
				}else if(_text=="two_four_text"){
					if(_str=="add"){
						flexBook.two_four_text+=1;
					}else{
						flexBook.two_four_text-=1;
					}
				}else if(_text=="three_four_text"){
					if(_str=="add"){
						flexBook.three_four_text+=1;
					}else{
						flexBook.three_four_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_four_text+=1;
					}else{
						flexBook.four_four_text-=1;
					}
				}
			}else if(_XX<405){
				_text+="_five_text";
				if(_text=="one_five_text"){
					if(_str=="add"){
						flexBook.one_five_text+=1;
					}else{
						flexBook.one_five_text-=1;
					}
				}else if(_text=="two_five_text"){
					if(_str=="add"){
						flexBook.two_five_text+=1;
					}else{
						flexBook.two_five_text-=1;
					}
				}else if(_text=="three_five_text"){
					if(_str=="add"){
						flexBook.three_five_text+=1;
					}else{
						flexBook.three_five_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_five_text+=1;
					}else{
						flexBook.four_five_text-=1;
					}
				}
			}else if(_XX<486){
				_text+="_six_text";
				if(_text=="one_six_text"){
					if(_str=="add"){
						flexBook.one_six_text+=1;
					}else{
						flexBook.one_six_text-=1;
					}
				}else if(_text=="two_six_text"){
					if(_str=="add"){
						flexBook.two_six_text+=1;
					}else{
						flexBook.two_six_text-=1;
					}
				}else if(_text=="three_six_text"){
					if(_str=="add"){
						flexBook.three_six_text+=1;
					}else{
						flexBook.three_six_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_six_text+=1;
					}else{
						flexBook.four_six_text-=1;
					}
				}
			}else if(_XX<567){
				_text+="_seven_text";
				if(_text=="one_seven_text"){
					if(_str=="add"){
						flexBook.one_seven_text+=1;
					}else{
						flexBook.one_seven_text-=1;
					}
				}else if(_text=="two_seven_text"){
					if(_str=="add"){
						flexBook.two_seven_text+=1;
					}else{
						flexBook.two_seven_text-=1;
					}
				}else if(_text=="three_seven_text"){
					if(_str=="add"){
						flexBook.three_seven_text+=1;
					}else{
						flexBook.three_seven_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_seven_text+=1;
					}else{
						flexBook.four_seven_text-=1;
					}
				}
			}else{
				_text+="_eigth_text";
				if(_text=="one_eigth_text"){
					if(_str=="add"){
						flexBook.one_eigth_text+=1;
					}else{
						flexBook.one_eigth_text-=1;
					}
				}else if(_text=="two_eigth_text"){
					if(_str=="add"){
						flexBook.two_eigth_text+=1;
					}else{
						flexBook.two_eigth_text-=1;
					}
				}else if(_text=="three_eigth_text"){
					if(_str=="add"){
						flexBook.three_eigth_text+=1;
					}else{
						flexBook.three_eigth_text-=1;
					}
				}else{
					if(_str=="add"){
						flexBook.four_eigth_text+=1;
					}else{
						flexBook.four_eigth_text-=1;
					}
				}
			}**/
		}
		private function makeImg(posictionX:int,posictionY:int,bitmap:ImageLoader):Image{
			var x1:Number =-posictionX,    y1:Number =-posictionY,    z1:Number = 30,    t1:Number =1; 
			var x2:Number =posictionX,    y2:Number =-posictionY,    z2:Number =-30,    t2:Number = 1; 
			var x3:Number =posictionX,    y3:Number =posictionY,    z3:Number =-30,    t3:Number = 1; 
			var x4:Number =-posictionX,    y4:Number =posictionY,    z4:Number = 30,    t4:Number = 1; 
			var focalLength:Number =400;
			
			var sprite:Sprite=new Sprite();
			var img:Image=new Image();
			sprite.x=posictionX;
			sprite.y=posictionY;
			bitmapData=bitmap.bitmap.bitmapData;
			
			var vertices:Vector.<Number> = new Vector.<Number>(); 
			vertices.push(x1*t1,y1*t1, x2*t2,y2*t2, x3*t3,y3*t3, x4*t4,y4*t4); 
			
			var indices:Vector.<int> =  new Vector.<int>(); 
			indices.push(0,1,3, 1,2,3); 
			
			var uvtData:Vector.<Number> = new Vector.<Number>(); 
			uvtData.push(0,0,t1, 1,0,t2, 1,1,t3, 0,1,t4); 
			//绘制一个图形
			sprite.graphics.beginBitmapFill(bitmapData);
			//修改上面的图形，把他变成一个任意形状的图形
			sprite.graphics.drawTriangles(vertices, indices, uvtData); 
			//将对象添加到指定的容易里面
			img.addChild(sprite);
			return img;
		}
		private function makeBorder(_x1:int,_x2:int,_x3:int,_x4:int,
									_y1:int,_y2:int,_y3:int,_y4:int,
									positionX:int,positionY:int,
									bitmap:ImageLoader):Image{
			var x1:Number =-_x1,   y1:Number =-_y1,    z1:Number = 30,    t1:Number =1; 
			var x2:Number =_x2,    y2:Number =-_y2,    z2:Number =-30,    t2:Number = 1; 
			var x3:Number =_x3,    y3:Number =_y3,    z3:Number =-30,    t3:Number = 1; 
			var x4:Number =-_x4,   y4:Number =_y4,    z4:Number = 30,    t4:Number = 1; 
			var focalLength:Number =400;
			
			var sprite:Sprite=new Sprite();
			var img:Image=new Image();
			sprite.x=positionX;
			sprite.y=positionY;
			bitmapData=bitmap.bitmap.bitmapData;
			
			var vertices:Vector.<Number> = new Vector.<Number>(); 
			vertices.push(x1*t1,y1*t1, x2*t2,y2*t2, x3*t3,y3*t3, x4*t4,y4*t4); 
			
			var indices:Vector.<int> =  new Vector.<int>(); 
			indices.push(0,1,3, 1,2,3); 
			
			var uvtData:Vector.<Number> = new Vector.<Number>(); 
			uvtData.push(0,0,t1, 1,0,t2, 1,1,t3, 0,1,t4); 
			//绘制一个图形
			sprite.graphics.beginBitmapFill(bitmapData);
			//修改上面的图形，把他变成一个任意形状的图形
			sprite.graphics.drawTriangles(vertices, indices, uvtData); 
			//将对象添加到指定的容易里面
			img.addChild(sprite);
			return img;
		}
	}
}